// pages/product/product.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //图标
    iconPath: app.globalData.iconPath,
    //访问java后台
    goJavaBackstagePath: app.globalData.goJavaBackstagePath,
    //测试数据图片
    testDataImgPath: app.globalData.testDataImgPath,

    typeProductList:[],
    selectorId:-1,

    orderId:null,
    orderStatus:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var orderId = options.orderId;
    var orderStatus = options.orderStatus;
    this.setData({
      orderId:orderId,
      orderStatus:orderStatus
    });
    //请求到后台获取订单详情页面数据
    // wx.request({
    //   url: this.data.goJavaBackstagePath+'/order/getWXUserOrderInfo',
    //   data:{orderId:orderId},
    //   header: {
    //     'content-type': 'application/json'
    //   },
    //   success: function (res) {
    //     console.log(res.data)
    //   }
    // })
      var typeProductList=[{
        id: 1, name: '相框', products: [
          { id: 1, url: this.data.testDataImgPath+'/mei1.jpg', name: '相框产品一', price: 120, productNum: 1, oneProductPhotoNum: 1, photoNum: 1 },
          { id: 2, url: this.data.testDataImgPath +'/mei2.jpg', name: '相框产品二', price: 110, productNum: 2, oneProductPhotoNum: 1, photoNum: 2 },
          { id: 3, url: this.data.testDataImgPath +'/mei3.jpg', name: '相框产品三', price: 130, productNum: 3, oneProductPhotoNum: 1, photoNum: 3 },
        ]
      }, {
        id: 2, name: '海报', products: [
          { id: 4, url: this.data.testDataImgPath +'/mei4.jpg', name: '海报产品一', price: 80, productNum: 2, oneProductPhotoNum: 2, photoNum: 1 },
          { id: 5, url: this.data.testDataImgPath +'/mei5.jpg', name: '海报产品二', price: 90, productNum: 2, oneProductPhotoNum: 3, photoNum: 2 },
          { id: 6, url: this.data.testDataImgPath +'/mei6.jpg', name: '海报产品三', price: 100, productNum: 3, oneProductPhotoNum: 1, photoNum: 2 },
        ]
      }, {
        id: 3, name: '相册', products: [
          { id: 7, url: this.data.testDataImgPath +'/mei7.jpg', name: '相册产品一', price: 50, productNum: 1, oneProductPhotoNum: 3, photoNum: 3 },
          { id: 8, url: this.data.testDataImgPath +'/mei8.jpg', name: '相册产品二', price: 60, productNum: 1, oneProductPhotoNum: 3, photoNum: 2 },
          { id: 9, url: this.data.testDataImgPath +'/mei9.jpg', name: '相册产品三', price: 30, productNum: 1, oneProductPhotoNum: 3, photoNum: 2 },
          { id: 13, url: this.data.testDataImgPath +'/mei7.jpg', name: '相册产品四', price: 50, productNum: 1, oneProductPhotoNum: 3, photoNum: 2  },
          { id: 14, url: this.data.testDataImgPath +'/mei8.jpg', name: '相册产品五', price: 60, productNum: 1, oneProductPhotoNum: 3, photoNum: 2  },
          { id: 15, url: this.data.testDataImgPath +'/mei9.jpg', name: '相册产品六', price: 30, productNum: 1, oneProductPhotoNum: 3, photoNum: 2  },
          { id: 16, url: this.data.testDataImgPath +'/mei7.jpg', name: '相册产品七', price: 50, productNum: 1, oneProductPhotoNum: 3, photoNum: 2  },
          { id: 17, url: this.data.testDataImgPath +'/mei8.jpg', name: '相册产品八', price: 60, productNum: 1, oneProductPhotoNum: 3, photoNum: 2  },
          { id: 18, url: this.data.testDataImgPath +'/mei9.jpg', name: '相册产品九', price: 30, productNum: 1, oneProductPhotoNum: 3, photoNum: 2  },
        ]
      }, {
        id: 4, name: '钥匙扣', products: [
          { id: 10, url: this.data.testDataImgPath +'/mei10.jpg', name: '钥匙扣产品一', price: 10, productNum: 1, oneProductPhotoNum: 3, photoNum: 2 },
          { id: 11, url: this.data.testDataImgPath +'/mei11.jpg', name: '钥匙扣产品二', price: 20, productNum: 1, oneProductPhotoNum: 2, photoNum: 2 },
          { id: 12, url: this.data.testDataImgPath +'/mei12.jpg', name: '钥匙扣产品三', price: 30, productNum: 1, oneProductPhotoNum: 2, photoNum: 2 },
          { id: 10, url: this.data.testDataImgPath +'/mei10.jpg', name: '钥匙扣产品一', price: 10, productNum: 1, oneProductPhotoNum: 3, photoNum: 2 },
          { id: 11, url: this.data.testDataImgPath +'/mei11.jpg', name: '钥匙扣产品二', price: 20, productNum: 1, oneProductPhotoNum: 2, photoNum: 2 },
          { id: 12, url: this.data.testDataImgPath +'/mei12.jpg', name: '钥匙扣产品三', price: 30, productNum: 1, oneProductPhotoNum: 2, photoNum: 2 },
          { id: 10, url: this.data.testDataImgPath +'/mei10.jpg', name: '钥匙扣产品一', price: 10, productNum: 1, oneProductPhotoNum: 3, photoNum: 2 },
          { id: 11, url: this.data.testDataImgPath +'/mei11.jpg', name: '钥匙扣产品二', price: 20, productNum: 1, oneProductPhotoNum: 2, photoNum: 2 },
          { id: 12, url: this.data.testDataImgPath +'/mei12.jpg', name: '钥匙扣产品三', price: 30, productNum: 1, oneProductPhotoNum: 2, photoNum: 2 },
        ]
      }];
      this.createTip(typeProductList);
      this.setData({ typeProductList: typeProductList});
  },
  triggerEvent: function () {},
  openType:function(event){
    var selectorId=event.target.dataset.typeid;
    if (this.data.selectorId == selectorId) {
      selectorId = -1;
    }
    this.setData({ selectorId: selectorId});
  },
  //页面跳转至产品详情页面
  toProductDetail:function(event){
    var productId = event.target.dataset.productid;
    wx.navigateTo({ url: '/pages/productDetail/productDetail?orderId=' + this.data.orderId + '&productId=' + productId+'&orderStatus='+this.data.orderStatus})
  },
  //跳转至验片页面
  goRemark(event){
    var productId = event.target.dataset.productid;
    wx.navigateTo({
      url: '../remark/remark?orderId=' + this.data.orderId + '&productId=' + productId  + '&orderStatus=' + this.data.orderStatus,
    })
  },
  //---------------菜单跳转

  //跳转订单列表
  toOrder:function(){
    wx.redirectTo({ url: '/pages/order/order' })
  },
  //跳转到产品库  
  toProductRepository: function (event) {
    if(this.data.orderStatus<=1){
      wx.redirectTo({ url: '/pages/productRepository/productRepository?orderId=' + this.data.orderId})
    }else{
      wx.showModal({
        title: '提示',
        content: '订单已结存，无法进行产品添加！',
      })
    }   
  },
  //跳转到照片库
  toPhotoRepository: function () {
    console.log(this.data.orderStatus)
    if(this.data.orderStatus==0){
      wx.showModal({
        title: '提示',
        content: '当前订单，照片还未上传！无法查看！',
      })
    }else{
      wx.redirectTo({ url: '/pages/photoRepository/photoRepository?orderId=' + this.data.orderId + '&orderStatus=' + this.data.orderStatus});
    }
    
  },
  //去到支付页面
  toPay(event){
    if (this.createTip(this.data.typeProductList)&&this.data.orderStatus<2){
      wx.navigateTo({
        url: '../pay/pay?orderId=' + this.data.orderId,
      })
    } else if (this.createTip(this.data.typeProductList)==false && this.data.orderStatus < 2){
      var that = this;
      wx.showModal({
        title: '提示',
        content: '您选片未完成，请查看产品列表，是否确认支付！',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../pay/pay?orderId=' + that.data.orderId,
            })
          }
        }
      })
    }else if(this.data.orderStatus==2){
      wx.navigateTo({
        url: '../pay/pay?orderId=' + this.data.orderId,
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '您已支付！',       
      })
    }  
  },

  previewProduct(event) {
    var current = event.target.dataset.current;
    wx.previewImage({ current: current, urls: [current] })
  },
  /** 循环type下面的产品 **/
  createTip(ptypeList){
    var complete = true;
    for(var j=0;j<ptypeList.length;j++){
      var chooseOK=true;      
      var ptype = ptypeList[j];
      for(var i=0;i<ptype.products.length;i++){
        var couldCount = ptype.products[i].productNum * ptype.products[i].oneProductPhotoNum;
        if (couldCount > ptype.products[i].photoNum) { chooseOK = false; complete=false;}
        if (chooseOK) { ptype.tip = '选择完毕' }
        if(!chooseOK){ptype.tip='待选'}
      }
    }
    return complete;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})